import { constantRoutes, asyncRoutes } from '@/router'

export default {
  namespaced: true,
  state() {
    return {
      routes: constantRoutes // 静态加动态
    }
  },
  mutations: {
    setRoutes(state, otherRoutes) {
      state.routes = [...constantRoutes, ...otherRoutes]
    }
  },
  actions: {
    filterRoutes({ commit }, menus) {
      // console.log({ asyncRoutes, menus }) // asyncRoutes动态路由  menus代表可访问的动态页面
      /*
      过滤出我们的路由
       1,调用mutations 添加到vuex里
       2,return返回就可以拿到数据addRoutes(过滤后的路由)
      */
      const otherRoutes = asyncRoutes.filter(item => {
        // console.log('item', item)
        if (menus.includes(item.children[0].name)) {
          return true
        } else {
          return false
        }
      })
      commit('setRoutes', otherRoutes) // 调用mutations中的方法，把过滤出的可访问的路由保存vuex
      return otherRoutes
    }
  }
}
